package com.example.casper.Experiment2024.gone;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.casper.Experiment2024.R;
import com.example.casper.Experiment2024.items.Location;

import java.util.List;

public class RecyclerViewLocationAdapter extends RecyclerView.Adapter<RecyclerViewLocationAdapter.LocationViewHolder> {

    private final Context context;
    private final List<Location> list;

    public RecyclerViewLocationAdapter(Context context, List<Location> list) {
        this.context = context;
        this.list = list;
    }

    public void updateList(List<Location> newLocations) {
        this.list.clear();
        this.list.addAll(newLocations);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public LocationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.location_item, parent, false);
        return new LocationViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull LocationViewHolder holder, int position) {
        Location location = list.get(position);
        if (location.getPicPath() != null) {
            Glide.with(context).load(location.getPicPath()).into(holder.imageViewPic);
        } else {
            holder.imageViewPic.setImageResource(R.drawable.foot);
        }
        holder.textViewLocationName.setText(location.getLocationName());
        holder.textViewLatitude.setText(String.valueOf(location.getLatitude()));
        holder.textViewLongitude.setText(String.valueOf(location.getLongitude()));
        holder.textViewFeeling.setText(location.getFeeling());
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, goneLocationDetailsActivity.class);
            intent.putExtra("location", location);
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class LocationViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageViewPic;
        public TextView textViewLocationName, textViewLatitude, textViewLongitude, textViewFeeling;

        public LocationViewHolder(@NonNull View itemView) {
            super(itemView);
            imageViewPic = itemView.findViewById(R.id.image_view_pic);
            textViewLocationName = itemView.findViewById(R.id.text_view_location_name);
            textViewLatitude = itemView.findViewById(R.id.text_view_latitude);
            textViewLongitude = itemView.findViewById(R.id.text_view_longitude);
            textViewFeeling = itemView.findViewById(R.id.text_view_feeling);
        }
    }
}